<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Car_brand_model extends CI_Model {

 
    function __construct()
    {
         parent::__construct();
    }
	
	function select_list()
	{
		 $this->db->from('base_carbrand');
		 $this->db->order_by("brandname", "asc"); 


		$query = $this->db->get();

		$result=$query->result_array();

		return $result;

	}

	function select_page($data = array(
			'brandname'=>''  //品牌名称
			,'start'=>0		//开始记录数
			,'length'=>0))	//长度
	{
		if($data['brandname'])
		{
			$this->db->like('brandname',$data['brandname']);
		}

	    $this->db->from('base_carbrand');

		//查询总记录数
		$count = $this->db->count_all_results();

		$result['count'] = $count;
		/////////////////////////////////////////////////////////////////
		if($data['brandname'])
		{
			$this->db->like('brandname',$data['brandname']);
		}

	    $this->db->from('base_carbrand');

		if(intval($data['length'])<0)
		{
			$data['length'] =10;
		}
		if($data['start'])
		{
			$this->db->limit($data['length'],$data['start']);
		}
		else
		{
			$this->db->limit($data['length'],0);
		}
		$this->db->order_by("brandname", "asc"); 


		$query = $this->db->get();

		$result['list']=$query->result_array();

		return $result;
	}


	function select_by_id($brandid)
	{
		$this->db->where('brandid',$brandid);
		$query = $this->db->get('base_carbrand');
		$row = $query->row_array();
		return $row;
	}

	function update($brandid,$data=array())
	{
		$fields = array();
		if($data['brandname'])
		{
			$fields['brandname'] = $data['brandname'];
		}
		if($data['brandinfo'])
		{
			$fields['brandinfo'] = $data['brandinfo'];
		}
		if($data['brandlogo'])
		{
			$fields['brandlogo'] = $data['brandlogo'];
		}
		$this->db->where('brandid', $brandid);
		return $this->db->update('base_carbrand', $fields); 
	}

	function insert($data=array())
	{
		$fields = array();
		if($data['brandname'])
		{
			$fields['brandname'] = $data['brandname'];
		}
		if($data['brandinfo'])
		{
			$fields['brandinfo'] = $data['brandinfo'];
		}
		if($data['brandlogo'])
		{
			$fields['brandlogo'] = $data['brandlogo'];
		}
		$result = $this->db->insert('base_carbrand', $fields); 

		return $result;
	}

	function delete($brandid)
	{
		$result =array('success'=>true);
 
		$this->db->where('brandid',$brandid);

	    $this->db->from('base_carinfo');

		//查询总记录数
		$count = $this->db->count_all_results();

		if($count >0 )
		{
			$result['success']=false;
			$result['message'] ='删除车辆品牌信息失败，车系已在使用，请先删除对应的车辆信息！';
			return $result;
		}

		$this->db->where('brandid',$brandid);
		$result = $this->db->delete('base_carbrand'); 
		return $result;
	}
}